//
//  JXNavigationVC.swift
//  WBTest
//
//  Created by Fantasy on 2016/12/8.
//  Copyright © 2016年 Fantasy. All rights reserved.
//

import UIKit

class JXNavigationVC: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.isHidden = true
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // root时也会调用push压栈
        if childViewControllers.count > 0 {
            // 隐藏底部bar
            viewController.hidesBottomBarWhenPushed = true
            
            // 判断控制器的类型
            if let vc = viewController as? JXBaseViewController {
                var title = "返回"
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                // 取出自定义的 navItem，设置左侧按钮作为返回按钮
//                title: title, style: .plain, target: self, action: #selector(popToParent)
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
            }
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToParent() {
        popViewController(animated: true)
    }

}
